/**
 * @author Administrator
 * 多选择人员树
 */
var selectuserradiowin,endselectuserradiowin,selectuserradioidtager='',userradiosm,radiooldnode=0, 
selectuserradionametager='',selectradiousersort='',selectuserradionodetager,
radiouserid=new Ext.data.Store();
function initselectuserradio(){
		 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";

	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/departmenttree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,
	        height:420,
	        width:230,
	        anchor:'0% 100%'
	    });
	tree.setRootNode(root); 
	tree.on('click',function(node){
		selectuserradioarray()
		selectradiousersort.load({
			params:{departmentid:node.id},
			callback:function(r,options,success){
				for(var i=0;i<r.length;i++)
				{
					var index = radiouserid.findBy(function(record,id){
						if(record.data.nodeid == node.id && record.data.userId ==  r[i].data.userId)
						{
							userradiosm.selectRecords([r[i]],true);
						}
					});
				}
				var girdEl = grid.getEl();
				girdEl.select('div.x-grid3-hd-checker').removeClass('x-grid3-hd-checker');
			} 
		});
		radiooldnode=node.id;
	})
	/**
	 * 用户列表
	 */
	var userlisturl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectUserByDepartmentList.do'
	selectradiousersort = new Ext.data.JsonStore({
		url:userlisturl,
		fields:['userId','uuid','userName']
	});
	userradiosm  = new Ext.grid.CheckboxSelectionModel({
		handleMouseDown:Ext.emptyFn,
		singleSelect :true,
		onMouseDown:function(e,t){
			if(t.className == 'x-grid3-row-checker x-grid3-row-radioBtn'){
				 	e.stopEvent();
		            var row = e.getTarget('.x-grid3-row');
		            if(row){
		                var index = row.rowIndex;
		                if(this.isSelected(index)){
		                    this.deselectRow(index);
		                }else{
		                    this.selectRow(index, true);
		                }
		            }
			}
		}
	});
	var cm = new Ext.grid.ColumnModel([
		userradiosm,
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		sm:userradiosm,
		width:270,
		height:420,
		anchor:'100% 100%',
		store:selectradiousersort,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
	grid.store.on('load', function() {  //数据加载完毕替换为checkbox列增加一个class，然后我们在这个class中修改图片
	  grid.getEl().select("div[class=x-grid3-row-checker]").each(function(x) {
	    x.addClass('x-grid3-row-radioBtn');
	  });
	});
	selectuserradiowin = new Ext.Window({
		layout:'absolute',
		title:'人员选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid],
		buttonAlign:'right',
		buttons:[{
			text:'确定',
			handler:function(){
					 Ext.MessageBox.show({
			           title:'消息',
			           msg: '是否执行此操作?',
			           buttons: Ext.MessageBox.YESNO,
			           fn: function(btn){
			           	  if(btn == 'yes'){
			           		selectuserradioarray();
							var userid='',username='',nodeid='';
							radiouserid.each(function(record){
								userid=record.data.userId;
								username=record.data.userName;
								nodeid = record.data.nodeid;
							})
							$('#userid').attr('value',userid);
							$('#'+selectuserradionametager).attr('value',username);
							$('#'+selectuserradionodetager).attr('value',nodeid);
							selectuserradiowin.hide();
							changActor($('#taskid').attr('value'));
			           	  }	
			           },
			           icon: Ext.MessageBox.QUESTION
       });
				
			}
		},{
			text:'取消',
			handler:function(){
				selectuserradiowin.hide();		
			}
		}]
		
	})
}
/**
 * 打开人员的多选树
 * @param {} id
 * @param {} name
 */
function selectuserradiowinshow(taskid,id,name,nodeid){
	$('#taskid').attr('value',taskid);
	selectuserradioidtager = id;
	selectuserradionametager = name;
	selectuserradionodetager = nodeid;
	userradiosm,radiooldnode=0;
	selectradiousersort.removeAll();
	var userid = $('#'+selectuserradioidtager).attr('value');
	var username = $('#'+selectuserradionametager).attr('value');
	var nodeid = $('#'+selectuserradionodetager).attr('value');
	if(typeof(userid)   ==   'undefined')
	{
		radiouserid.removeAll();
	}
	else
	{
		
		radiouserid.removeAll();
		radiouserid.addSorted(new Ext.data.Record({
			nodeid:nodeid,
			userId:userid,
			userName:username
		}))
	}
	selectuserradiowin.show();
	selectuserradiowin.findByType('treepanel')[0].root.expand();  
}

/**
 * 添加数组中的数据
 * @param {} id
 */
function selectuserradioarray(){
	clearuserradioarray();
	var rlolesarray,nouserselectaray,index;
	rlolesarray = userradiosm.getSelections();
	for(var i =0;i<rlolesarray.length;i++)
	{
		index = radiouserid.findBy(function(record,id){
			 return record.data.nodeid == radiooldnode && rlolesarray[i].data.userId == record.data.userId	
		})
		radiouserid.removeAll();
		if(index == -1 ||radiouserid.getCount() ==0)
		{
			radiouserid.addSorted(new Ext.data.Record({
				nodeid:radiooldnode,
				userId:rlolesarray[i].data.userId,
				userName:rlolesarray[i].data.userName
			}))
		}
	}
	
}
/**
 * 从数组中清除未被选中的数据
 */
function clearuserradioarray(){
	selectradiousersort.each(function(r){
		if(userradiosm.isSelected(r) == false)
		{
			var index =	radiouserid.findBy(function(record,id){
				return  record.data.nodeid == radiooldnode && record.data.userId == r.data.userId
			})
			radiouserid.remove(radiouserid.getAt(index));
		}
	})
	
}